using Jupiter.Infrastructure.Permission;
using Jupiter.Infrastructure.Permission.Attributes;
using Jupiter.Infrastructure.Responses;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using SOP.DataTransfer.AppService.Authorities.Roles.Requests;
using Wips.AppService.Authorities.Roles;
using Wips.AppService.Authorities.Roles.Requests;
using Wips.AppService.Authorities.Roles.Responses;
using Wips.AppService.Authorities.Roles.ViewModels;
using Wips.AppService.CommonViewModels;

namespace Wips.MainServer.Controllers.Authorities
{
    /// <summary>
    /// 角色
    /// </summary>
    public class RoleController : CustomeBaseController
    {
        private readonly IRoleService _service;
        private readonly IMediator _mediator;
        private readonly ISecurityContextAccessor _contextAccessor;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="service"></param>
        /// <param name="mediator"></param>
        /// <param name="contextAccessor"></param>
        public RoleController(IRoleService service, IMediator mediator, ISecurityContextAccessor contextAccessor)
        {
            _service = service;
            _mediator = mediator;
            _contextAccessor = contextAccessor;
        }

        #region 基础接口

        /// <summary>
        /// 读取列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<PageResult<GetRolePagesResponse>> GetPagesAsync(
            [FromBody] GetRolePagesRequest request)
        {
            return await _service.GetPagesAsync(request);
        }

        /// <summary>
        /// 根据ID读取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<RoleViewModel> GetAsync([FromQuery] string id)
        {
            return await _service.GetByIdAsync(id);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<string> PostAsync([FromBody] CreateRoleRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission]
        public async Task<string> PutAsync([FromBody] UpdateRoleRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        [Permission]
        public async Task<string> DeleteAsync([FromBody] DeleteRoleRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 状态变更
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission]
        public async Task<string> StatusChangeAsync([FromBody] RoleStatusChangeRequest request)
        {
            return await _mediator.Send(request);
        }

      

    
       
        #endregion

        #region 扩展接口

        /// <summary>
        /// 读取角色数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<List<SelectViewModel>> GetSelectDataAsync()
        {
            return await _service.GetSelectDataAsync();
        }

        /// <summary>
        /// 根据角色Id读取职位Id列表
        /// </summary>
        /// <param name="positionId">职位ID</param>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<List<string>> GetIdsByPositionIdAsync([FromQuery] string positionId)
        {
            return await _service.GetIdsByPositionIdAsync(positionId);
        }

        /// <summary>
        /// 为角色分配职位
        /// </summary>
        /// <param name="request">请求主体</param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<bool> AssignPositionsAsync([FromBody] AssignPositionsForRoleRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 初始化管理员角色
        /// </summary>
        /// <param name="request">请求主体</param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<string> InitAdmin([FromBody] InitAdminRoleRequest request)
        {
            return await _mediator.Send(request);
        }


        #endregion
    }
}